package com.tledu.zyf.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.tledu.zyf.dao.BaseDao;
import com.tledu.zyf.dao.IGroupDao;
import com.tledu.zyf.model.Group;
import com.tledu.zyf.util.MyBatisUtil;

public class GroupDao extends BaseDao<Group> implements IGroupDao {

	@Override
	public List<Group> list() {
		return list(Group.class);
	}

	@Override
	public Group loadByGroupname(String groupname) {
		return loadByGroupname(Group.class.getName() + ".loadByGroupname", groupname);
	}

	public Group loadByGroupname(String name, String groupname) {
		SqlSession session = null;
		Group group = null;
		try {
			session = MyBatisUtil.getSession();
			group = session.selectOne(name, groupname);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSession(session);
		}
		return group;
	}

	@Override
	public void delete(int id) {
		delete(Group.class, id);
	}

	@Override
	public Group load(int id) {
		return load(Group.class, id);
	}

	@Override
	public List<Group> vagueGroupname(String groupname) {
		return vagueGroupname(Group.class.getName() + ".vagueGroupname", groupname);
	}

	public List<Group> vagueGroupname(String name, String groupname) {
		SqlSession session = null;
		List<Group> groups = null;
		Map<String, Object> params = new HashMap<String, Object>();
		try {
			session = MyBatisUtil.getSession();
			params.put("groupname", "%" + groupname + "%");
			groups = session.selectList(name, params);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSession(session);
		}
		return groups;
	}

}
